//
//  XmlParserDelegateMarked.m
//  FleaFinder
//
//  Created by Hieu Le on 4/9/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import "XmlParserDelegateMarked.h"
#import "Category.h"

@implementation XmlParserDelegateMarked

@synthesize currentMarked;

-(id)init
{
    self = [super init];
    
    if(self != nil){        
        tagId = @"id";
        tagMarked = @"marked";
        tagName = @"name";
        tagZip = @"zip";
        tagType = @"type";
        tagFeeType = @"feetype";
        tagRoad = @"road";
        tagCity = @"city";
        tagDateFrom = @"datefrom";
        tagDateTo = @"dateto";
        tagStartAt = @"startat";
        tagEndAt = @"endat";
        tagDesc = @"desc";
        tagCategories = @"categories";
        tagCategory = @"category";
        tagTitle = @"title";
        tagContact = @"contact";
        tagEmail = @"email";
        tagHomepage = @"homepage";
        tagPhone = @"tlf";
        tagFacebook = @"fb";
        tagTweeter = @"twt";
        tagStande = @"stande";
        
        isInsideContactTag = NO;
        isInsideCategoryTag = NO;
        isInsideMarketTag = NO;
        
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd.MM.yy"];
        
        currentTag = -1;
        newLine = '\n';
        plusSign = @"+";
    }
    
    return self;
}

-(void) parseXMLMarkedInfo: (NSString*) sourceUrl
{
    /*NSData* data =  [NSData dataWithContentsOfFile: sourceUrl];
    
    //NSURL *url = [[NSURL alloc] initWithString:@"http://www.w3schools.com/xml/note.xml"];
    //NSData* data =  [NSData dataWithContentsOfURL:url];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
    [xmlParser setDelegate:self];
    [xmlParser parse];*/
    
    NSURL *url = [[NSURL alloc] initWithString: sourceUrl];
    
    NSError *error;
    NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    
    dataString = [dataString stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    
    //NSLog(@"%@", dataString);
    
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
    [xmlParser setDelegate:self];
    [xmlParser parse];
}

#pragma implementation of NSXMLParserDelegate

-(void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    //NSLog(@"Error = %@", parseError);
}

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
    //NSLog(@"Start the document");
}

-(void) parserDidEndDocument:(NSXMLParser *)parser
{
    //NSLog(@"End the document");
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([tagMarked compare:elementName] == NSOrderedSame)
    {
        if(!currentMarked)
        {
            currentMarked = [[Marked alloc] init];
        }
        isInsideMarketTag = YES;
    }
    else if([tagId compare:elementName] == NSOrderedSame)
    {
        if(isInsideCategoryTag)
            currentTag = MARKED_TAG_CATEGORY_ID;
        else
            currentTag = MARKED_TAG_ID;
    }
    else if([tagName compare:elementName] == NSOrderedSame)
    {
        if(isInsideContactTag)
        {
            currentTag = MARKED_TAG_CONTACT_NAME;
        }
        else
        {
            currentTag = MARKED_TAG_NAME;
        }
    }
    else if([tagType compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_TYPE;
    }
    else if([tagFeeType compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_FEE_TYPE;
    }
    else if([tagRoad compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_ROAD;
    }
    else if([tagCity compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CITY;
    }
    else if([tagZip compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKET_TAG_ZIP;
    }
    else if([tagDateFrom compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_DATE_FROM;
    }
    else if([tagDateTo compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_DATE_TO;
    }
    else if([tagStartAt compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_START_AT;
    }
    else if([tagEndAt compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_END_AT;
    }
    else if([tagStande compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKET_TAG_STANDE;
    }
    else if([tagDesc compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_DESC;
    }
    else if([tagCategories compare:elementName] == NSOrderedSame)
    {
        if(isInsideMarketTag){
            currentTag = MARKED_TAG_CATEGORIES;
            currentMarked.categories = [NSMutableArray arrayWithCapacity:5];
        }
    }
    else if([tagCategory compare:elementName] == NSOrderedSame)
    {
        if(isInsideMarketTag)
        {
            currentTag = MARKED_TAG_CATEGORY;
            isInsideCategoryTag = YES;
            
            currentCategory = [[Category alloc] init];
            [currentMarked.categories addObject:currentCategory];
        }
    }
    else if([tagTitle compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CATEGORY_TITLE;
    }
    else if([tagContact compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CONTACT;
        isInsideContactTag = YES;
        currentMarked.contact = [[Contact alloc] init];
    }
    else if([tagEmail compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CONTACT_EMAIL;
    }
    else if([tagHomepage compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CONTACT_HOMEPAGE;
    }
    else if([tagPhone compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CONTACT_TLF;
    }
    else if([tagFacebook compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CONTACT_FB;
    }
    else if([tagTweeter compare:elementName] == NSOrderedSame)
    {
        currentTag = MARKED_TAG_CONTACT_TWT;
    }
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    currentTag = -1;
    
    if([tagContact compare:elementName] == NSOrderedSame)
    {
        isInsideContactTag = NO;
    }
    else if([tagCategory compare:elementName] == NSOrderedSame)
    {
        isInsideCategoryTag = NO;
    }
    else if([tagMarked compare:elementName] == NSOrderedSame)
    {
        isInsideMarketTag = NO;
    }
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //NSLog(@"Character: %@", string);
    
    if([string characterAtIndex:0] == newLine && currentTag != MARKED_TAG_DESC)
    {
        return;
    }
    
    if(currentTag == MARKED_TAG_NAME)
    {
        if(!currentMarked.markedname)
            currentMarked.markedname = string;
        else
            currentMarked.markedname = [NSString stringWithFormat:@"%@%@", currentMarked.markedname, string];
    }
    else if(currentTag == MARKED_TAG_ID)
    {
        currentMarked.marketId = [string intValue];
    }
    else if(currentTag == MARKED_TAG_TYPE)
    {
        //currentMarked.type = string;
        if(!currentMarked.type)
            currentMarked.type = string;
        else
            currentMarked.type = [NSString stringWithFormat:@"%@%@", currentMarked.type, string];
    }
    else if(currentTag == MARKED_TAG_FEE_TYPE)
    {
        if(!currentMarked.feetype)
            currentMarked.feetype = string;
        else
            currentMarked.feetype = [NSString stringWithFormat:@"%@%@", currentMarked.feetype, string];
        
        if([string rangeOfString:plusSign].location != NSNotFound){
            currentMarked.isBusiness = NO;
        } else {
            currentMarked.isBusiness = YES;
        }
    }
    else if(currentTag == MARKED_TAG_ROAD)
    {
        if(!currentMarked.road)
            currentMarked.road = string;
        else
            currentMarked.road = [NSString stringWithFormat:@"%@%@", currentMarked.road, string];
    }
    else if(currentTag == MARKED_TAG_CITY)
    {
        if(!currentMarked.city)
            currentMarked.city = string;
        else
            currentMarked.city = [NSString stringWithFormat:@"%@%@", currentMarked.city, string];
    }
    else if(currentTag == MARKET_TAG_ZIP)
    {
        currentMarked.zip = string;
    }
    else if(currentTag == MARKED_TAG_DATE_FROM)
    {
        currentMarked.datefrom = string;
        
        NSDate *dateFrom = [dateFormatter dateFromString:string];
        currentMarked.datefrom_num = dateFrom.timeIntervalSince1970;
    }
    else if(currentTag == MARKED_TAG_DATE_TO)
    {
        currentMarked.dateto = string;
        
        NSDate *dateTo = [dateFormatter dateFromString:string];
        currentMarked.dateto_num = dateTo.timeIntervalSince1970;
    }
    else if(currentTag == MARKED_TAG_START_AT)
    {
        currentMarked.startat = string;
    }
    else if(currentTag == MARKED_TAG_END_AT)
    {
        currentMarked.endat = string;
    }
    else if(currentTag == MARKET_TAG_STANDE)
    {
        currentMarked.stande = string;
    }
    else if(currentTag == MARKED_TAG_DESC)
    {
        if(!currentMarked.desc)
            currentMarked.desc = string;
        else
            currentMarked.desc = [NSString stringWithFormat:@"%@%@", currentMarked.desc, string];
    }
    /*else if(currentTag == MARKED_TAG_CATEGORIES)
    {
        if(!currentMarked.categories)
        {
            currentMarked.categories = [NSMutableArray arrayWithCapacity:5];
        }
    }*/
    /*else if(currentTag == MARKED_TAG_CATEGORY)
    {
        //[currentMarked.categories addObject:string];
    }*/
    else if(currentTag == MARKED_TAG_CATEGORY_ID)
    {
        currentCategory.categoryId = [string intValue];
    }
    else if(currentTag == MARKED_TAG_CATEGORY_TITLE)
    {
        if(!currentCategory.title)
            currentCategory.title = string;
        else
            currentCategory.title = [NSString stringWithFormat:@"%@%@", currentCategory.title, string];
    }
    else if(currentTag == MARKED_TAG_CONTACT)
    {
        if(!currentMarked.contact)
        {
            currentMarked.contact = [[Contact alloc] init];
        }
    }
    else if(currentTag == MARKED_TAG_CONTACT_NAME)
    {
        if(!currentMarked.contact.contactname)
            currentMarked.contact.contactname = string;
        
        else
            currentMarked.contact.contactname = [NSString stringWithFormat:@"%@%@", currentMarked.contact.contactname, string];
    }
    else if(currentTag == MARKED_TAG_CONTACT_EMAIL)
    {
        if(!currentMarked.contact.email)
            currentMarked.contact.email = string;
            
        else
            currentMarked.contact.email = [NSString stringWithFormat:@"%@%@", currentMarked.contact.email, string];
    }
    else if(currentTag == MARKED_TAG_CONTACT_HOMEPAGE)
    {
        currentMarked.contact.homepage = string;
    }
    else if(currentTag == MARKED_TAG_CONTACT_TLF)
    {
        currentMarked.contact.phone = string;
    }
    else if(currentTag == MARKED_TAG_CONTACT_FB)
    {
        currentMarked.contact.facebook = string;
    }
    else if(currentTag == MARKED_TAG_CONTACT_TWT)
    {
        currentMarked.contact.tweeter = string;
        /*if([currentMarked.contact.tweeter compare:@""] == NSOrderedSame){
            
        }*/
    }
}

@end
